package com.holdtime.jtt809dataservice.business.hd.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.holdtime.jtt809dataservice.business.hd.BusinessHdCoast;
import com.holdtime.jtt809dataservice.business.hd.entity.DsParentPlatformDO;
import com.holdtime.jtt809dataservice.business.hd.entity.mapper.DsParentPlatformMapper;
import com.holdtime.jtt809dataservice.business.hd.model.vo.DsParentPlatformPageReqVO;
import com.holdtime.jtt809dataservice.business.hd.model.vo.DsParentPlatformSaveReqVO;
import com.holdtime.jtt809dataservice.business.hd.service.DsParentPlatformService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.holdtime.jtt809dataservice.business.hd.model.enums.ErrorCodeConstants.DS_PARENT_PLATFORM_NOT_EXISTS;

/**
 * 上级平台信息; Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class DsParentPlatformServiceImpl implements DsParentPlatformService {

    @Resource
    private DsParentPlatformMapper dsParentPlatformMapper;

    @Override
    public String createDsParentPlatform(DsParentPlatformSaveReqVO createReqVO) {
        // 插入
        DsParentPlatformDO dsParentPlatform = BeanUtils.toBean(createReqVO, DsParentPlatformDO.class);
        dsParentPlatformMapper.insert(dsParentPlatform);
        // 返回
        return dsParentPlatform.getId();
    }

    @Override
    public void updateDsParentPlatform(DsParentPlatformSaveReqVO updateReqVO) {
        // 校验存在
        validateDsParentPlatformExists(updateReqVO.getId());
        // 更新
        DsParentPlatformDO updateObj = BeanUtils.toBean(updateReqVO, DsParentPlatformDO.class);
        dsParentPlatformMapper.updateById(updateObj);
    }

    @Override
    public void deleteDsParentPlatform(String id) {
        // 校验存在
        validateDsParentPlatformExists(id);
        // 删除
        dsParentPlatformMapper.deleteById(id);
    }

    private void validateDsParentPlatformExists(String id) {
        if (dsParentPlatformMapper.selectById(id) == null) {
            throw exception(DS_PARENT_PLATFORM_NOT_EXISTS);
        }
    }

    @Override
    public DsParentPlatformDO getDsParentPlatform(String id) {
        return dsParentPlatformMapper.selectById(id);
    }

    @Override
    public PageResult<DsParentPlatformDO> getDsParentPlatformPage(DsParentPlatformPageReqVO pageReqVO) {
        return dsParentPlatformMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DsParentPlatformDO> getDsParentPlatformList() {
        return dsParentPlatformMapper.selectList(
                Wrappers.<DsParentPlatformDO>lambdaQuery()
                        .eq(DsParentPlatformDO::getStatus, BusinessHdCoast.PARENT_PLATFORM_STATUS_OPEN)
        );
    }
}